<?php
/**
 * Created by PhpStorm.
 * User: wangwenfan
 * Date: 2018/7/31
 * Time: 15:15.
 */

namespace App\Admin\Controllers;

use App\Models\Base\PayAnalyze;
use App\Service\ChannelService;
use App\Service\ServerService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class PayAnalyController extends Controller
{
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('game.data_statistic'));
            $content->description(trans('game.analysis'));

            $content->body($this->grid());
            $this->filterScript();
        });
    }

    protected function grid()
    {
        $grid = Admin::grid(PayAnalyze::class, function (Grid $grid) {
            $grid->model()->orderBy('date_time', 'desc');
            $get = request()->all();
            $type = isset($get['type']) ? $get['type'] : 0;
            if (!$type) {
                $grid->model()->where('type', 1);
            }

            $grid->date_time(trans('admin.date'))->sortable();
            if (2 == $type) {
                $grid->channel()->name(trans('game.channel'));
            }
            if (3 == $type) {
                $grid->server()->name(trans('game.server_name'));
            }
            $grid->new_users(trans('game.new_user'));
            $grid->first_day_amount(trans('game.day_top-up'));
            $grid->total_amount(trans('game.top-up-30'));
            $grid->one_day_ltv(trans('game.day_ltv'));
            $grid->two_day_ltv(trans('game.two_day_ltv'));
            $grid->three_day_ltv(trans('game.three_day_ltv'));
            $grid->four_day_ltv(trans('game.4day_ltv'));
            $grid->five_day_ltv(trans('game.5day_ltv'));
            $grid->six_day_ltv(trans('game.6day_ltv'));
            $grid->seven_day_ltv(trans('game.7day_ltv'));
            $grid->fifteen_day_ltv(trans('game.15day_ltv'));
            $grid->thirty_day_ltv(trans('game.30day_ltv'));
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
            $grid->filter(function ($filter) use ($type) {
                $params = request()->all();
                $filter->disableIdFilter();
                $filter->between('date_time', trans('game.date'))->datetime();
                $filter->equal('type', trans('game.type'))->radio([1 => trans('game.platform_all'), 2 => trans('game.channel'), 3 => trans('game.server_name')]);
                $filter->equal('channel_id', '渠道')->select(ChannelService::getChannelList());
                $filter->equal('server_id', '游戏服')->select(ServerService::getServerList());
                $fileter_data = [];
                if (1 == $type) {
                    $filter->setNotWhere(['server_id', 'channel_id']);
                } elseif (2 == $type) {
                    if (isset($params['channel_id']) && 99 == $params['channel_id']) {
                        $fileter_data = ['channel_id'];
                    }
                    $fileter_data[] = 'server_id';
                } else {
                    if (isset($params['server_id']) && 99 == $params['server_id']) {
                        $fileter_data = ['server_id'];
                    }
                    $fileter_data[] = 'channel_id';
                }
                $filter->setNotWhere($fileter_data);
            });
        });

        return $grid;
    }
}
